package UI.Main;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
/**
 * @author 杨明政
 * @version  2021.0510
 * @since 1.8
 */

/**
 * 自定义Jlist组成.
 * For example:
 * <pre>
 * MyCellRenderer render=new MyCellRenderer(icons);
 * </pre>
 * @author 杨明政
 * @see javax.swing.JComponent
 * @see javax.swing.JLabel
 * @see javax.swing.ListCellRenderer
 * @see javax.swing.SwingConstants
 * @see javax.swing.TransferHandler
 */
public class MyCellRenderer extends JLabel implements ListCellRenderer {
    Icon[] icons;
    
    public MyCellRenderer() {};
    /**
     * 生成自定义组件
     * @param icons 已经获得的图标
     */
    public MyCellRenderer(Icon[] icons) {
        // TODO Auto-generated constructor stub
        this.icons = icons;
    }
    /**
     * 获得list列表组件
     * @param list 列表
     * @param value 当前对象
     * @param index 序号
     * @param isSelected 是否被选中
     * @param cellHasFocus 是否获得焦点
     */
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        String s = value.toString();
        setText(s);
        setSize(100,100);
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));// 加入宽度为5的空白边框
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }setIcon(icons[index]);// 设置图片
        setEnabled(list.isEnabled());
        setFont(list.getFont());
        setOpaque(true);
        return this;
    }
    
}
